package com.sansui.blog.service.impl;

import com.sansui.blog.dao.UserDao;
import com.sansui.blog.pojo.User;
import com.sansui.blog.pojo.UserLoginLog;
import com.sansui.blog.pojo.sys.SysRole;
import com.sansui.blog.pojo.vo.UpdatePassword;
import com.sansui.blog.service.UserService;
import com.sansui.blog.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @name: UserServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-19 22:36
 **/

/*用户信息获取service 实现类*/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> getList() {
        return null;
//        return sysUserMapper.getList();
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public Map<String, Object> getUserList(Integer page, Integer limit) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            Integer pages = (page-1)*limit;
            Integer userCount = userDao.countUser();
            List<User> users = userDao.getUserList(pages, limit);
            data.put("code",0);
            data.put("msg","查询成功！");
            data.put("count",userCount);
            data.put("data",users);
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","查询失败！");
        }
        return data;
    };

    @Transactional
    @Override
    public Map<String, Object> updateUserStatus(Long id , String status){
        Map<String,Object> data = new HashMap<>(16);
        try{
            User user = userDao.getUserById(id);
            if(user == null){
                data.put("code",401);
                data.put("msg","修改失败,用户不存在！");
                return data;
            }
            Integer updateNum = userDao.updateUserStatus(id,status);
            if(updateNum != 1){
                data.put("code",402);
                data.put("msg","修改失败！");
                return data;
            }
            data.put("code",200);
            data.put("msg","修改成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","修改失败！");
        }
        return data;
    }

    /**
     * 根据用户名和密码查询用户信息
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     */
    @Override
    public User checkUser(String username, String password) {
        // 获取uuid
        String uuid = userDao.getUserUuid(username);
        String psw = password+uuid;
        // 查询用户信息
        User user = userDao.queryUserByUserName(username, Md5.getMd5(psw));
        return user;

    }

    @Transactional
    @Override
    public void insertLoginLog(String username, String ip) {
        String uuid = userDao.getUserUuid(username);
        UserLoginLog loginLog = new UserLoginLog();
        loginLog.setUid(uuid);
        loginLog.setUsername(username);
        loginLog.setLastLoginIp(ip);
        loginLog.setLastLoginTime(new Date());
        userDao.insertLoginLog(loginLog);
    }

    @Transactional
    @Override
    public Map<String, Object> updatePassword(UpdatePassword updatePassword, String uid){
        Map<String,Object> data = new HashMap<>(16);
        try{

            String old_password = updatePassword.getOld_password();
            String new_password = updatePassword.getNew_password();
            String again_password = updatePassword.getAgain_password();
            User user = userDao.getUserByUid(uid);

            if( user == null ){
                data.put("code",401);
                data.put("msg","修改失败,用户不存在！");
                return data;
            }

            if(!Md5.getMd5(old_password+uid).equals(user.getPassword())){
                data.put("code",402);
                data.put("msg","修改失败,旧密码输入错误,请检查！");
                return data;
            }

            if(!new_password.equals(again_password)){
                data.put("code",403);
                data.put("msg","修改失败,确认密码与新密码不一致,请检查！");
                return data;
            }

            if(user.getPassword().equals(Md5.getMd5(again_password+uid))){
                data.put("code",404);
                data.put("msg","修改失败,新密码与旧密码一致！");
                return data;
            }

            String pattern="^(?=.*[a-zA-Z])(?=.*\\d).{1,9}$";
//            String pattern="^(?=.*[a-zA-Z])(?=.*\\d).{6,12}$";
//            pattern = "^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";

            if(!Pattern.compile(pattern).matcher(again_password).matches()){
                data.put("code",405);
                data.put("msg","密码应为6到12位，包含数字和字母！");
                return data;
            }

            userDao.updatePassword(Md5.getMd5(again_password+user.getUid()),user.getUid());
            data.put("code",200);
            data.put("msg","修改成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","密码修改失败！");
        }
        return data;
    }



    @Override
    public Map<String, Object> getLoginLog(){
        Map<String,Object> data = new HashMap<>(16);
        List<UserLoginLog> userlogs = userDao.getLoginLog();
        data.put("code",0);
        data.put("msg","查询成功！");
        data.put("data",userlogs);
        return data;
    }





}
